package view;

import controller.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.information.Contact;
import model.information.ContactPhoneNumber;

/**
 *
 * @author tobias
 */
public class ContactPanel extends javax.swing.JPanel {
    private Controller ctrl;
    private ArrayList<Contact> contacts;
    private Contact currentContact;


    /** Creates new form ContactPane */
    public ContactPanel(Controller ctrl) {
        initComponents();
        this.ctrl = ctrl;
        contacts = ctrl.getContacts();
        updateList();
        
    }

    public void updateList() {
        contactList.setListData(contacts.toArray());
    }

    public void updateFromSave(Object o) {
        contactList.repaint();
    }

    public void addPhoneNumber(String countryCode, String phoneNumber, ContactPhoneNumber.PhoneType phoneType) {
        currentContact.addPhoneNumber(new ContactPhoneNumber(phoneType, countryCode, phoneNumber));
        phoneList.setListData(currentContact.getPhoneNumbers().toArray());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jLabel1 = new javax.swing.JLabel();
        listPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        contactList = new javax.swing.JList();
        addContactBtn = new javax.swing.JButton();
        delBtn = new javax.swing.JButton();
        contactPanel = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        firstNameField = new javax.swing.JTextField();
        postalcodeField = new javax.swing.JTextField();
        addressField = new javax.swing.JTextField();
        delPhoneBtn = new javax.swing.JButton();
        cityField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        phoneList = new javax.swing.JList();
        emailField = new javax.swing.JTextField();
        lastNameField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        addPhoneBtn = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        saveBtn = new javax.swing.JButton();
        countryField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();

        jScrollPane3.setViewportView(jTree1);

        setMaximumSize(new java.awt.Dimension(1000, 560));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(816, 586));

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+3));
        jLabel1.setText("Kontaktpersoner");

        listPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Kontaktliste:"));
        listPanel.setOpaque(false);
        listPanel.setPreferredSize(new java.awt.Dimension(230, 518));

        contactList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        contactList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                contactListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(contactList);

        addContactBtn.setText("Opret ny kontakt");
        addContactBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addContactBtnActionPerformed(evt);
            }
        });

        delBtn.setText("Slet kontakt");
        delBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout listPanelLayout = new org.jdesktop.layout.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(delBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(addContactBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE))
                .addContainerGap())
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addContactBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(delBtn)
                .addContainerGap())
        );

        contactPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Kontaktperson:"));
        contactPanel.setOpaque(false);
        contactPanel.setPreferredSize(new java.awt.Dimension(532, 518));

        jLabel7.setText("By");

        jLabel8.setText("E-mail");

        jLabel2.setText("Telefonnumre");

        firstNameField.setEnabled(false);

        postalcodeField.setEnabled(false);

        addressField.setEnabled(false);

        delPhoneBtn.setText("Slet Nummer");
        delPhoneBtn.setEnabled(false);
        delPhoneBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delPhoneBtnActionPerformed(evt);
            }
        });

        cityField.setEnabled(false);

        phoneList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        phoneList.setEnabled(false);
        jScrollPane1.setViewportView(phoneList);

        emailField.setEnabled(false);

        lastNameField.setEnabled(false);

        jLabel4.setText("Efternavn");

        jLabel3.setText("Fornavn");

        addPhoneBtn.setText("Tilføj Nummer");
        addPhoneBtn.setEnabled(false);
        addPhoneBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPhoneBtnActionPerformed(evt);
            }
        });

        jLabel5.setText("Adresse");

        jLabel6.setText("Postnummer");

        saveBtn.setText("Gem ændringer");
        saveBtn.setEnabled(false);
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        countryField.setEnabled(false);

        jLabel9.setText("Land");

        org.jdesktop.layout.GroupLayout contactPanelLayout = new org.jdesktop.layout.GroupLayout(contactPanel);
        contactPanel.setLayout(contactPanelLayout);
        contactPanelLayout.setHorizontalGroup(
            contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(contactPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(contactPanelLayout.createSequentialGroup()
                        .add(5, 5, 5)
                        .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel6)
                            .add(jLabel9)
                            .add(jLabel8)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lastNameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                            .add(countryField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                            .add(firstNameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                            .add(contactPanelLayout.createSequentialGroup()
                                .add(postalcodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel7)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cityField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE))
                            .add(emailField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, addressField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, contactPanelLayout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(delPhoneBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(addPhoneBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, saveBtn))
                .addContainerGap())
        );
        contactPanelLayout.setVerticalGroup(
            contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(contactPanelLayout.createSequentialGroup()
                .add(20, 20, 20)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE, false)
                    .add(jLabel3)
                    .add(firstNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE, false)
                    .add(jLabel4)
                    .add(lastNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addressField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(postalcodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6)
                    .add(cityField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel7))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(countryField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel9))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(emailField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(contactPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(contactPanelLayout.createSequentialGroup()
                        .add(addPhoneBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(delPhoneBtn))
                    .add(jLabel2)
                    .add(jScrollPane1, 0, 0, Short.MAX_VALUE))
                .add(18, 18, Short.MAX_VALUE)
                .add(saveBtn)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(listPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(contactPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE))
                    .add(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(contactPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                    .add(listPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void delBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delBtnActionPerformed
        if(contactList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt en kontaktperson der skal slettes, vælg venligst en kontakt, og prøv igen.", "Vælg kontakt", JOptionPane.WARNING_MESSAGE);
        } else {
            ctrl.removeContact(currentContact);
        }
}//GEN-LAST:event_delBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        if(firstNameField.getText().equals("")) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Et eller flere felter er ikke udfyldt korrekt, udfyld venligst et fornavn, og prøv igen.", "Manglende oplysninger", JOptionPane.WARNING_MESSAGE);
        } else {
            currentContact.setFirstName(firstNameField.getText());
            currentContact.setLastName(lastNameField.getText());
            currentContact.setAdress(addressField.getText());
            currentContact.setPostalCode(postalcodeField.getText());
            currentContact.setCity(cityField.getText());
            currentContact.setCountry(countryField.getText());
            currentContact.setEmail(emailField.getText());
        }
}//GEN-LAST:event_saveBtnActionPerformed

    private void addContactBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addContactBtnActionPerformed
        Contact newContact = ctrl.addContact("Ny", "Kontakt");
        contactList.setSelectedValue(newContact, true);
        firstNameField.requestFocus();
}//GEN-LAST:event_addContactBtnActionPerformed

    private void contactListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_contactListValueChanged
        if(contactList.getSelectedIndex() != -1) {
            currentContact = (Contact)contactList.getSelectedValue();
            firstNameField.setText(currentContact.getFirstName());
            firstNameField.setEnabled(true);
            lastNameField.setText(currentContact.getLastName());
            lastNameField.setEnabled(true);
            addressField.setText(currentContact.getAdress());
            addressField.setEnabled(true);
            postalcodeField.setText(currentContact.getPostalCode());
            postalcodeField.setEnabled(true);
            cityField.setText(currentContact.getCity());
            cityField.setEnabled(true);
            countryField.setText(currentContact.getCountry());
            countryField.setEnabled(true);
            emailField.setText(currentContact.getEmail());
            emailField.setEnabled(true);
            phoneList.setListData(currentContact.getPhoneNumbers().toArray());
            phoneList.setEnabled(true);
            saveBtn.setEnabled(true);
            addPhoneBtn.setEnabled(true);
            delPhoneBtn.setEnabled(true);
        } else {
            firstNameField.setText("");
            firstNameField.setEnabled(false);
            lastNameField.setText("");
            lastNameField.setEnabled(false);
            addressField.setText("");
            addressField.setEnabled(false);
            postalcodeField.setText("");
            postalcodeField.setEnabled(false);
            cityField.setText("");
            cityField.setEnabled(false);
            countryField.setText("");
            countryField.setEnabled(false);
            emailField.setText("");
            emailField.setEnabled(false);
            phoneList.setListData(new Object[0]);
            phoneList.setEnabled(false);
            saveBtn.setEnabled(false);
            addPhoneBtn.setEnabled(false);
            delPhoneBtn.setEnabled(false);
        }
}//GEN-LAST:event_contactListValueChanged

    private void addPhoneBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPhoneBtnActionPerformed
        PhoneDialog pd = new PhoneDialog(ctrl.getFrame(), true, this);
        pd.setVisible(true);
    }//GEN-LAST:event_addPhoneBtnActionPerformed

    private void delPhoneBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delPhoneBtnActionPerformed
        if(phoneList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt et telefonnummer der skal slettes, vælg venligst et telefonnummer, og prøv igen.", "Vælg telefonnummer", JOptionPane.WARNING_MESSAGE);
        } else {
            ContactPhoneNumber currentPhone = (ContactPhoneNumber)phoneList.getSelectedValue();
            currentContact.removePhoneNumber(currentPhone);
            phoneList.setListData(currentContact.getPhoneNumbers().toArray());
        }
    }//GEN-LAST:event_delPhoneBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addContactBtn;
    private javax.swing.JButton addPhoneBtn;
    private javax.swing.JTextField addressField;
    private javax.swing.JTextField cityField;
    private javax.swing.JList contactList;
    private javax.swing.JPanel contactPanel;
    private javax.swing.JTextField countryField;
    private javax.swing.JButton delBtn;
    private javax.swing.JButton delPhoneBtn;
    private javax.swing.JTextField emailField;
    private javax.swing.JTextField firstNameField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTree jTree1;
    private javax.swing.JTextField lastNameField;
    private javax.swing.JPanel listPanel;
    private javax.swing.JList phoneList;
    private javax.swing.JTextField postalcodeField;
    private javax.swing.JButton saveBtn;
    // End of variables declaration//GEN-END:variables

}
